package com.tengroup.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tengroup.entity.*;
import com.tengroup.service.*;
import com.tengroup.vo.Answervo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.tengroup.vo.difficultyvo;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/haosirun")
public class haosirun {
    @Autowired
    private DifficultyService difficultyService;

    @Autowired
    private QuestionTypeService questionTypeService;

    @Autowired
    private SubjectTypeService subjectTypeService;

    @Autowired
    private ExamService examService;

    @Autowired
    private RuleService ruleService;

    @Autowired
    private ExampaperService exampaperService;

    @Autowired
    private ExampaperServicempl exampaperservice;

    @PostMapping("/insertdifficulty")
    public String insertdifficulty(@RequestBody DifficultyEntity difficulty) {
        int i= difficultyService.insert(difficulty);
        if(i==1){
            return "success";
        }
        else{
            return "fail";
        }
    }

    @PostMapping("/updatedifficulty")
    public String updatedifficulty(@RequestBody DifficultyEntity difficulty) {
        int i= difficultyService.update(difficulty);
        if(i==1){
            return "success";
        }
        else{
            return "fail";
        }
    }

    @DeleteMapping("/deletedifficulty")
    public String deleteDifficulty(@RequestParam int i) {
        int h= difficultyService.delete(i);
        if(h==1){
            return "success";
        }
        else{
            return "fail";
        }
    }

    @PostMapping("/insertQuestionType")
    public String insertQuestionType(@RequestBody QuestionTypeEntity questionType) {
        int i= questionTypeService.insert(questionType);
        if(i==1){
            return "success";
        }
        else{
            return "fail";
        }
    }

    @DeleteMapping("/deleteQuestionType/{id}")
    public String deleteQuestionType(@PathVariable int id) {
        int i= questionTypeService.delete(id);
        if(i==1){
            return "success";
        }
        else{
            return "fail";
        }
    }

    @PostMapping("/updatequestiontype")
    public String updatequestiontype(@RequestBody QuestionTypeEntity questionType) {
        int i= questionTypeService.update(questionType);
        if(i==1){
            return "success";
        }
        else{
            return "fail";
        }
    }
    @GetMapping("/getTypeByType")
    public List<QuestionTypeEntity> getTypeByType(@RequestParam String type) {
        return questionTypeService.getTypeByType(type);
    }

    @GetMapping("/question/{questiontypeid}")
    public QuestionTypeEntity getquestiontype(@PathVariable int questiontypeid) {
        return questionTypeService.getById(questiontypeid);
    }

    @GetMapping("/questionpage")
    public Page<QuestionTypeEntity> listquestiontype(@RequestParam(defaultValue = "1")int pagenum, @RequestParam(defaultValue = "10")int pagesize) {
        return questionTypeService.page(new Page<>(pagenum, pagesize));
    }

    @PostMapping("insertsubjecttype")
    public String insertsubjecttype(@RequestBody SubjectTypeEntity subjectType) {
        int i= subjectTypeService.insert(subjectType);
        if(i==1){
            return "success";
        }
        else{
            return "fail";
        }
    }

    @DeleteMapping("deletesubjecttype")
    public String deletesubjecttype(@RequestBody SubjectTypeEntity subjectType) {
        int i= subjectTypeService.delete(subjectType.getid());
        if(i==1){
            return "success";
        }
        else{
            return "fail";
        }
    }

    @PostMapping("updatesubjecttype")
    public String updatesubjecttype(@RequestBody SubjectTypeEntity subjectType) {
        int i= subjectTypeService.update(subjectType);
        if(i==1){
            return "success";
        }
        else{
            return "fail";
        }
    }

    @GetMapping("/getsubject{subjecttypeid}")
    public SubjectTypeEntity getsubjecttype(@PathVariable int subjecttypeid) {
        return subjectTypeService.getById(subjecttypeid);
    }

    @GetMapping("/subjectpage{subjecttypepage}")
    public Page<SubjectTypeEntity> listsubjectivetype(@RequestParam(defaultValue = "1")int pagenum, @RequestParam(defaultValue = "10")int pagesize) {
        return subjectTypeService.page(new Page<>(pagenum, pagesize));
    }

    @PostMapping("/generate")
    public String generate(@RequestParam int ruleid) {
            try{
            examService.save(ruleService.getById(ruleid));
        }
        catch(Exception e){
                return "fail";
        }
        return "success";
    }

    @GetMapping ("search")
    public List<Question> searchquestions(
            @RequestParam(required = false)String question_type,
            @RequestParam(required = false)String difficulty,
            @RequestParam(required = false)Integer subject_id,
            @RequestParam(defaultValue = "10")int limit
    ){
        return examService.findbyconditions(question_type, difficulty, subject_id,limit);
    }

    @GetMapping("selectbyrule")
    public List<Question> selectbyrule(@RequestParam int id) {
        return examService.selectQuestionsByRule(ruleService.getById(id));
    }

    @GetMapping("getexampaper")
    public ExampaperEntity getexampaper(@RequestParam int id) {
        return exampaperService.getById(id);
    }

    @PostMapping("/answer")
    public String Answer(@RequestBody Answervo answervo) {
         try{
            ExampaperEntity e= examService.setanswer(answervo);
            exampaperservice.updateById(e);
         }
        catch(Exception e){
            return "fail";
        }
         return "success";
    }

    @PostMapping("/ceshi")
    public List<ExampaperepEntity> ceshi(){
        return examService.findbyexampaperid();
    }

    @PostMapping("/judge")
    public String judge(@RequestParam int i) {
        try {
            ExampaperEntity s=examService.judge(i);
            exampaperservice.updateById(s);
        }
        catch(Exception e){
            return "fail";
        }
        return "success";
    }

}
